//
//  NSDate+Expend.m
//  expend
//
//  Created by ZAK on 14-3-27.
//  Copyright (c) 2014年 JKZL. All rights reserved.
//
#define DB_TimeType @"yyyy-MM-dd HH:mm:ss.SSS"
#import "NSDate+Expend.h"
static NSDateFormatter * formatter = nil;
@implementation NSDate (Expend)

+(NSString *)returnCreditsExchangeProductTime:(NSDate *)timeDate
{
    
    NSCalendar *greCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *dateComponents = [greCalendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSWeekCalendarUnit | NSWeekdayCalendarUnit | NSWeekOfMonthCalendarUnit | NSWeekOfYearCalendarUnit fromDate:timeDate];
    
    NSDateComponents *todateComponents = [greCalendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSWeekCalendarUnit | NSWeekdayCalendarUnit | NSWeekOfMonthCalendarUnit | NSWeekOfYearCalendarUnit fromDate:[NSDate date]];
    
    
    if (dateComponents.day > todateComponents.day) {
        return [NSString stringWithFormat:@"明日:%@ 开抢",[timeDate dataToStringWithChangeType:@"HH:mm"]];
    }else
    {
        return [NSString stringWithFormat:@"今日:%@ 开抢",[timeDate dataToStringWithChangeType:@"HH:mm"]];
    }
}




+(NSString *)getMiddleAndSecond:(NSString *)dateString
{
    NSDate *date = [NSDate stringToDate:dateString withChangeType:DB_TimeType];
    return [date dataToStringWithChangeType:@"HH:mm"];
}

+(NSString *)getWeekDate:(NSString *)dateString
{
    NSDate *date = [NSDate stringToDate:dateString withChangeType:DB_TimeType];
    if (date) {
        NSCalendar *greCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
        NSDateComponents *dateComponents = [greCalendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSWeekCalendarUnit | NSWeekdayCalendarUnit | NSWeekOfMonthCalendarUnit | NSWeekOfYearCalendarUnit fromDate:date];
        switch (dateComponents.weekday) {
            case 1:
            {
                return @"周日";
                break;
            }
            case 2:
            {
                return @"周一";
                break;
            }
            case 3:
            {
                return @"周二";
                break;
            }
            case 4:
            {
                return @"周三";
                break;
            }
            case 5:
            {
                return @"周四";
                break;
            }
            case 6:
            {
                return @"周五";
                break;
            }
            case 7:
            {
                return @"周六";
                break;
            }
            default:
                break;
        }
        return @"错误日期";
    }else
    {
        return @"错误日期";
    }
}


+(NSDate *)stringToDate:(NSString *)date withChangeType:(NSString *)formatterType
{
    if (date.length > 1) {
//        NSRange rg = [date rangeOfString:@"." options:NSBackwardsSearch];
//        if (rg.length >0 ) {
//            date = [date substringToIndex:rg.length];
//        }
        NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
        [formatter setDateFormat:formatterType];
//        [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:8]];
        NSDate *changeDate = [formatter dateFromString:date];
        if (changeDate) {
            return changeDate;
        }else
        {
            DEBUG_Log(@"%@",@"类型错误");
            return nil;
        }
        
    }else
    {
        return nil;
    }
   
}
+(NSString *)localMediaDatetime
{
    NSDate  *nowDate = [NSDate date];
    NSString *curDateTime = [nowDate dataToStringWithChangeType:@"yyyyMMddHHmm"];
    return curDateTime;
}

+(NSString *)localDatetime
{
    NSDate  *nowDate = [NSDate date];
    NSString *curDateTime = [nowDate dataToStringWithChangeType:@"yyyy-MM-dd HH:mm"];
    return curDateTime;
}

-(NSString *)dataToStringWithChangeType:(NSString *)formatterType
{
    if (self != nil) {
        if (formatter == nil)
        {
            formatter = [[NSDateFormatter alloc]init];
        }
        [formatter setLocale:[NSLocale currentLocale]];
        [formatter setDateFormat:formatterType];
        NSString *dateStr = [formatter stringFromDate:self];
        if (dateStr) {
            return dateStr;
        }
        else
        {
            DEBUG_Log(@"%@",@"类型错误");
            return @"";
        }
    }else
    {
        return @"";
    }
}

//获取当前月天数
+(NSInteger)returnDefineMouthOfDays:(NSInteger)mouth currentDate:(NSDate *)date
{
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents* comps = [[NSDateComponents alloc]init];
    [comps setMonth:mouth];
    [comps setDay:1];
    date = [calendar dateByAddingComponents:comps toDate:date options:0];
    
    return [NSDate numberDaysInMonthOfDate:date];
}
//获取当前月天数
+ (NSInteger)numberDaysInMonthOfDate:(NSDate *)date_

{
    
    NSCalendar *calender = [NSCalendar currentCalendar];
    
    NSRange range = [calender rangeOfUnit:NSDayCalendarUnit
                     
                                   inUnit: NSMonthCalendarUnit
                     
                                  forDate: date_];
    return range.length;
    
}



//修改日期
+(NSDate *)returnChangeDate:(NSInteger)day mouth:(NSInteger)mouth currentDate:(NSDate *)date
{
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents* comps = [[NSDateComponents alloc]init];
    [comps setMonth:mouth];
    [comps setDay:day];
    date = [calendar dateByAddingComponents:comps toDate:date options:0];
    
    return date ;
}
//获取循环次数
+(NSInteger)needCirlceCount:(NSDate *)beginDate endDate:(NSDate *)endDate betweenDays:(NSInteger)days
{
    NSString *begin = [beginDate dataToStringWithChangeType:@"yyyy年MM月dd日"];
    beginDate = [NSDate stringToDate:begin withChangeType:@"yyyy年MM月dd日"];
    
    NSString *end = [endDate dataToStringWithChangeType:@"yyyy年MM月dd日"];
    endDate = [NSDate stringToDate:end withChangeType:@"yyyy年MM月dd日"];
    
    NSTimeInterval timeBetween = [endDate timeIntervalSinceDate:beginDate];
    return (NSInteger)(timeBetween / (days * 60 *60 *24));
}
//获取当前周开始日
+(NSDate *)getWeekBegin:(NSDate *)newDate{
    if (newDate == nil) {
        newDate = [NSDate date];
    }
    double interval = 0;
    NSDate *beginDate = nil;
    NSCalendar *calendar = [NSCalendar currentCalendar];
    [calendar setFirstWeekday:2];
    [calendar rangeOfUnit:NSWeekCalendarUnit startDate:&beginDate interval:&interval forDate:newDate];
    return beginDate;
    
}
////获取当月的某一日时间段
//+(NSDate *)createNewWeekDay:(int)day current:(NSDate *)date
//{
//    NSCalendar *greCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
//    //  定义一个NSDateComponents对象，设置一个时间点
//    //    NSDateComponents *dateComponentsForDate = [[NSDateComponents alloc] init];
//    [greCalendar setFirstWeekday:1];
//    NSDateComponents *dateComponents = [greCalendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSWeekCalendarUnit | NSWeekdayCalendarUnit | NSWeekOfMonthCalendarUnit | NSWeekOfYearCalendarUnit fromDate:date];
//    [dateComponents setWeekday:day];
//    NSDate *dateFromDateComponentsForDate = [greCalendar dateFromComponents:dateComponents];
//    [greCalendar release];
//    return dateFromDateComponentsForDate;
//}


//获取当月的某一日时间段
+(NSDate *)createNewDateDay:(int)day current:(NSDate *)date
{
    NSCalendar *greCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    //  定义一个NSDateComponents对象，设置一个时间点
    //    NSDateComponents *dateComponentsForDate = [[NSDateComponents alloc] init];
    NSDateComponents *dateComponents = [greCalendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSWeekCalendarUnit | NSWeekdayCalendarUnit | NSWeekOfMonthCalendarUnit | NSWeekOfYearCalendarUnit fromDate:date];
    [dateComponents setDay:day];
    NSDate *dateFromDateComponentsForDate = [greCalendar dateFromComponents:dateComponents];
    
    return dateFromDateComponentsForDate;
}


+(NSInteger)getDateYear:(NSDate *)date
{
    if (date) {
        NSCalendar *greCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
        NSDateComponents *dateComponents = [greCalendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSWeekCalendarUnit | NSWeekdayCalendarUnit | NSWeekOfMonthCalendarUnit | NSWeekOfYearCalendarUnit fromDate:date];
        return dateComponents.year;
    }else
    {
        return 0;
    }
   
}
+(NSTimeInterval)timeDiff:(NSString *)older new:(NSString *)newer
{
    NSDate *oldDate = [NSDate stringToDate:older withChangeType:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *newDate = [NSDate stringToDate:newer withChangeType:@"yyyy-MM-dd HH:mm:ss"];
    NSTimeInterval timeDiff = 0.0f;
    timeDiff = [newDate timeIntervalSinceDate:oldDate];
    return timeDiff;
}
+(NSDate *)createTodayAlarmDate
{
    NSCalendar *nowGreCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *dateComponents = [nowGreCalendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSWeekCalendarUnit | NSWeekdayCalendarUnit | NSWeekOfMonthCalendarUnit | NSWeekOfYearCalendarUnit fromDate:[NSDate date]];
    NSCalendar *greCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *dateComponentsForDate = [[NSDateComponents alloc] init];
    [dateComponentsForDate setYear:dateComponents.year];
    [dateComponentsForDate setMonth:dateComponents.month];
    [dateComponentsForDate setDay:dateComponents.day];
    [dateComponentsForDate setHour:20];
    NSDate *dateFromDateComponentsForDate = [greCalendar dateFromComponents:dateComponentsForDate];
    //    [greCalendar release];
    return dateFromDateComponentsForDate;
}
+(NSDate *)createNewAlarmDate
{
    NSCalendar *nowGreCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *dateComponents = [nowGreCalendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSWeekCalendarUnit | NSWeekdayCalendarUnit | NSWeekOfMonthCalendarUnit | NSWeekOfYearCalendarUnit fromDate:[NSDate date]];
    NSCalendar *greCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *dateComponentsForDate = [[NSDateComponents alloc] init];
    [dateComponentsForDate setYear:dateComponents.year];
    [dateComponentsForDate setMonth:dateComponents.month];
    [dateComponentsForDate setDay:dateComponents.day +1];
    [dateComponentsForDate setHour:20];
     NSDate *dateFromDateComponentsForDate = [greCalendar dateFromComponents:dateComponentsForDate];
//    [greCalendar release];
    return dateFromDateComponentsForDate;
}

+(NSDate *)createNewDate:(NSInteger)year Mouth:(NSInteger)mouth Day:(NSInteger)day Hour:(NSInteger)hour Minute : (NSInteger)minute Second:(NSInteger)second
{
    NSCalendar *greCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    //  定义一个NSDateComponents对象，设置一个时间点
    NSDateComponents *dateComponentsForDate = [[NSDateComponents alloc] init];
    if (year >= 0) {
        [dateComponentsForDate setYear:year];
    }
    if (day >= 0) {
         [dateComponentsForDate setDay:day];
    }
    if (hour >= 0) {
        [dateComponentsForDate setHour:hour];
    }
    if (mouth >= 0) {
        [dateComponentsForDate setMonth:mouth];
    }
    if (minute >= 0) {
        [dateComponentsForDate setMinute:minute];
    }
    if (second >= 0) {
        [dateComponentsForDate setSecond:second];
    }
    //  根据设置的dateComponentsForDate获取历法中与之对应的时间点
    //  这里的时分秒会使用NSDateComponents中规定的默认数值，一般为0或1。
    NSDate *dateFromDateComponentsForDate = [greCalendar dateFromComponents:dateComponentsForDate];
//    NSDateComponents *dateComponentsAsTimeQantum = [[NSDateComponents alloc] init];
//    [dateComponentsForDate setDay:6];
//    //  在当前历法下，获取6天后的时间点
//    NSDate *dateFromDateComponentsAsTimeQantum = [greCalendar dateByAddingComponents:dateComponentsAsTimeQantum toDate:[NSDate date] options:0];
    
    
    return dateFromDateComponentsForDate;
}
+(NSString *)timeDes:(NSInteger)time
{
    if (time >= 0 && time < 6) {
        return @"凌晨";
    }
    if (time >= 6 && time < 12) {
        return @"早上";
    }
    if (time >= 12 && time < 13) {
        return @"中午";
    }
    if (time >= 13 && time < 19) {
        return @"下午";
    }
    if (time >= 19 && time < 24) {
        return @"晚上";
    }
    return @"";
}

+(NSDate *)createNewDate:(NSInteger)addDay hour:(NSInteger)hour
{
    NSCalendar *nowGreCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *dateComponents = [nowGreCalendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSWeekCalendarUnit | NSWeekdayCalendarUnit | NSWeekOfMonthCalendarUnit | NSWeekOfYearCalendarUnit fromDate:[NSDate date]];
    NSCalendar *greCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *dateComponentsForDate = [[NSDateComponents alloc] init];
    [dateComponentsForDate setYear:dateComponents.year];
    [dateComponentsForDate setMonth:dateComponents.month];
    [dateComponentsForDate setDay:dateComponents.day +addDay];
//    [dateComponentsForDate setHour:dateComponents.hour+hour];
    [dateComponentsForDate setHour:hour];

//    [dateComponentsForDate setMinute:dateComponents.minute+2];
    NSDate *dateFromDateComponentsForDate = [greCalendar dateFromComponents:dateComponentsForDate];
    //    [greCalendar release];
    return dateFromDateComponentsForDate;
}


+(NSString *)toWeichatTime:(NSString *)time
{
    NSString *sTimeWeichat = nil;
    
    if (time.length < 11 || time == nil) {
        return @"";
    }
    NSRange rg = [time rangeOfString:@"." options:NSBackwardsSearch];
    if(rg.location > 0 && rg.location < time.length)
    {
        time = [time substringToIndex:rg.location-1];
    }
    NSString *timeDay = [time substringToIndex:10];
    NSDate *curDate = [NSDate date];
    
    NSDateComponents *CurComponents = [[NSCalendar currentCalendar]
                                       components:
                                       NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit
                                       |NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit
                                       |NSWeekOfMonthCalendarUnit
                                       fromDate:curDate];
    
    NSInteger CurYear = [CurComponents year];
    NSInteger CurMonth = [CurComponents month];
//    int CurDay = [CurComponents day];
//    int CurWeek = [CurComponents weekday];
    NSInteger CurWeekOfMonth = [CurComponents weekOfMonth];
    
    NSString *nowDay = [curDate dataToStringWithChangeType:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *nowDate = [NSDate stringToDate:time withChangeType:@"yyyy-MM-dd HH:mm:ss"];
    NSDateComponents *NowComponents = [[NSCalendar currentCalendar] components:
                                       NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit
                                       |NSWeekdayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit
                                       |NSWeekOfMonthCalendarUnit
                                                                      fromDate:nowDate];
    NSInteger NowYear = [NowComponents year];
    NSInteger NowMonth = [NowComponents month];
    NSInteger NowDay = [NowComponents day];
    
    NSInteger NowHour = [NowComponents hour];
    NSInteger NowMM = [NowComponents minute];
//    int NowWeek = [NowComponents weekday];
    NSInteger NowWeekOfMonth = [NowComponents weekOfMonth];
    
    NSDate *tLeakDate=[[NSDate alloc]initWithTimeIntervalSinceNow:-24*60*60];
    NSString *lastDay = [tLeakDate dataToStringWithChangeType:@"yyyy-MM-dd"];
    if ([timeDay isEqualToString:lastDay])
    {
        //昨天 早上，下午
        NSString *strDate = [NSString stringWithFormat:
                             @"昨天 %@"
                             ,[NSDate timeDes:NowHour]
                             ];
        return strDate;
    }
    else if ([timeDay isEqualToString:nowDay]) {
        //"2013-12-12 10:46:04.0";
        //早上 下午
        NSString *strDate = [NSString stringWithFormat:@"%@ %ld:%ld",[NSDate timeDes:NowHour],(long)NowHour,(long)NowMM];
        return strDate;//[time substringWithRange:NSMakeRange(11, 5)];
    }
    else if(NowYear == CurYear && NowMonth == CurMonth && CurWeekOfMonth == NowWeekOfMonth)
    {
        //        NSDictionary *dicMap = @{@"1":@"日",@"2":@"一",@"3":@"二",@"4":@"三",@"5":@"四",@"6":@"五",@"7":@"六"};
        //        NSString *s = [NSString stringWithFormat:@"%d",NowWeek];
        //NSString *strDate = [NSString stringWithFormat:@"周%@ %@",dicMap[s],[UTLTool timeDes:NowHour]];
        
        NSDateFormatter *df = [[NSDateFormatter alloc] init];
        [df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        NSDate *date = [df dateFromString:time];
        [df setDateFormat:@"HH:mm"];
        NSString *dateString = [df stringFromDate:date];
        NSString *strDate = [NSString stringWithFormat:@"%@%@",[NSDate timeDes:NowHour], dateString];
        return strDate;
    }
    else if(CurYear == NowYear)
    {
        NSString *strDate = [NSString stringWithFormat:@"%ld月%ld日",(long)NowMonth,(long)NowDay];
        return strDate;
    }
    else
    {
        NSString *strDate = [NSString stringWithFormat:@"%ld年%ld月%ld日",(long)NowYear,(long)NowMonth,(long)NowDay];
        return strDate;
    }

    return sTimeWeichat;
}


@end
